package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpExprMapper empExprMapper;

    @Override
    public PageResult<Emp> pageEmp(EmpQueryParam param) {
        Page<Emp> page = Page.of(param.getPage(), param.getPageSize());
        page.addOrder(OrderItem.desc("update_time"));
        Page<Emp> empPage = lambdaQuery()
                .like(StringUtils.hasLength(param.getName()), Emp::getName, param.getName())
                .in(null != param.getGender(), Emp::getGender, param.getGender())
                .ge(null != param.getBegin(), Emp::getEntryDate, param.getBegin())
                .le(null != param.getEnd(), Emp::getEntryDate, param.getEnd())
                .page(page);
        List<Emp> records = empPage.getRecords();
        if (!CollectionUtils.isEmpty(records)) {
            Set<Integer> deptIds = records.stream().map(Emp::getDeptId).collect(Collectors.toSet());
            List<Dept> depts = deptMapper.selectBatchIds(deptIds);
            Map<Integer, String> deptMap = depts.stream().collect(Collectors.toMap(Dept::getId, Dept::getName));
            for (Emp record : records) {
                record.setDeptName(deptMap.get(record.getDeptId()));
            }
        }
        return new PageResult<>(empPage.getTotal(), records);
    }

    //保存员工信息和员工的工作经历信息
    @Transactional
    @Override
    public void saveEmp(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(emp.getCreateTime());
        //保存员工信息
        empMapper.insert(emp);
        //保存员工的工作经历信息
        List<EmpExpr> exprList = emp.getExprList();
        if (!CollectionUtils.isEmpty(exprList)) {
            exprList.forEach(e -> e.setEmpId(emp.getId()));
            empExprMapper.insert(exprList);
        }
    }

    //根据ID查询员工信息
    @Override
    public Emp getByEmpId(Integer id) {
        Emp emp = empMapper.selectById(id);

        if (null != emp.getDeptId()) {
            Dept dept = deptMapper.selectById(emp.getDeptId());
            emp.setDeptName(dept.getName());
        }
        LambdaQueryWrapper<EmpExpr> empExprWrapper = new LambdaQueryWrapper<>();
        empExprWrapper.eq(EmpExpr::getEmpId, emp.getId());
        List<EmpExpr> empExprs = empExprMapper.selectList(empExprWrapper);

        emp.setExprList(empExprs);
        return emp;
    }

    // 调用service层方法批量删除员工信息和员工工作经历信息
    @Transactional
    @Override
    public void deleteEmp(List<Integer> ids) {
        empMapper.deleteByIds(ids);
        empExprMapper.deleteByIds(ids);
    }

    // 调用service层方法修改员工信息
    @Override
    @Transactional
    public void updateEmp(Emp emp) {
        empMapper.updateById(emp);
        LambdaUpdateWrapper<EmpExpr> empExprWrapper = new LambdaUpdateWrapper<>();
        empExprWrapper.eq(EmpExpr::getEmpId, emp.getId());
        empExprMapper.delete(empExprWrapper);
        List<EmpExpr> exprList = emp.getExprList();
        if (!CollectionUtils.isEmpty(exprList)) {
            exprList.forEach(e -> e.setEmpId(emp.getId()));
            empExprMapper.insert(emp.getExprList());
        }
    }

    // 调用service层方法查询员工列表
    @Override
    public List<Emp> listEmp() {
        return list();
    }


}
